package cuiyt.juc.lock8;

import java.util.concurrent.TimeUnit;

/**
 * @author cyt
 * @create 2020-04-29 23:55
 */
public class test5 {
    public static void main(String[] args) throws InterruptedException {
        phone5 phone1 = new phone5();

        new Thread(() -> {
            try {
                phone1.sendMes();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "A").start();


        TimeUnit.SECONDS.sleep(1);
        new Thread(() -> {
            phone1.call();
        }, "B").start();
    }

}


class phone5 {

    // synchronized 会获得方法调用者的锁 ， 并且在方法结束之前一直持有。
    public static synchronized void sendMes() throws InterruptedException {

        TimeUnit.SECONDS.sleep(4);
        System.out.println("发短信");
    }

    public static synchronized void call() {
        System.out.println("打电话");
    }

}
